package models

import (
	"fmt"
	"log"
	"rongyy/src/app"
	"time"

	"git.inedcn.com/ined/cmn"
)

type PpdModel struct {
	Id        int64        `xorm:"pk autoincr" json:"id"`
	PatientId string       `json:"patient_id"`                // 患者号
	EpisodeNo string       `json:"episode_no"`                // 就诊号
	Type      string       `json:"type"`                      // jj甲基
	CreatedAt cmn.JsonTime `xorm:"created" json:"created_at"` // 创建时间
	UpdatedAt cmn.JsonTime `xorm:"updated" json:"updated_at"` // 更新时间
	Hospital  string       `json:"hospital" xorm:"-"`
}

type ReqPpdApply struct {
	PatientId               string `json:"patient_id"`
	Date                    string `json:"date"`
	Type                    string `json:"type"`
	IntracutaneousInjection string `json:"intracutaneousInjection"`
}

type PpdHisData struct {
	ARCIMCode      string      `json:"ARCIMCode"`
	OrderPrior     string      `json:"OrderPrior"`
	OrderStartDate interface{} `json:"OrderStartDate"`
	OrderStartTime interface{} `json:"OrderStartTime"`
	OrderRecLoc    string      `json:"OrderRecLoc"`
	InsuTypeCode   string      `json:"InsuTypeCode"`
	OrderPackQty   int         `json:"OrderPackQty"`
	DoseQty        string      `json:"DoseQty,omitempty"`
	DoseUOMCode    string      `json:"DoseUOMCode,omitempty"`
	OrderFreqCode  string      `json:"OrderFreqCode,omitempty"`
	OrderInstrCode string      `json:"OrderInstrCode,omitempty"`
	LabSpec        string      `json:"LabSpec"`
}
type ReqPpdHis struct {
	EpisodeNo interface{}  `json:"EpisodeNo"`
	UserCode  string       `json:"UserCode"`
	Data      []PpdHisData `json:"Data"`
}

func (_this *PpdModel) String() string {
	return fmt.Sprintf("userid:")
}

func (com *PpdModel) TableName() string {
	return "ppd_exam_apply"
}

func (com *PpdModel) Insert() error {
	_, err := app.DB().Insert(com)
	if err != nil {
		log.Println(err)
		return err
	}
	return nil
}

func (qm *PpdModel) FindTodayRecord(patientId string, ptype string) (*PpdModel, bool, error) {
	com := &PpdModel{}
	local, _ := time.LoadLocation("Asia/Shanghai")
	t := time.Now().In(local).Format("2006-01-02 15:04:05")
	has, err := app.DB().Where("patient_id = ? and TO_DAYS(created_at)=TO_DAYS(?)", patientId, t).Get(com)
	if err != nil {
		return nil, false, err
	}
	return com, has, nil
}

func (qm *PpdModel) TodayTotal(hospital, propType string) (int64, error) {
	com := &PpdModel{}
	local, _ := time.LoadLocation("Asia/Shanghai")
	t := time.Now().In(local).Format("2006-01-02 15:04:05")
	count, err := app.DB().Where("prop_type=?", propType).Where("TO_DAYS(created_at)=TO_DAYS(?)", t).Count(com)
	if err != nil {
		return 0, err
	}
	return count, nil
}
